package com.ctgu.frequence.step1;

import com.ctgu.frequence.Utils.ValueComparator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class CountWordLimitN {
    public static void countWordLimitN(String str,int n){
        Map<String,Integer> map = CountWord.getWordMap(str);
        //对map进行排序
        List<Map.Entry<String,Integer>> ls=new ArrayList<>();
        ls.addAll(map.entrySet());
        ValueComparator vc=new ValueComparator();
        Collections.sort(ls,vc);
        int k = 0;
        for(Map.Entry<String, Integer> item:ls){
            System.out.println(item.getKey()+"出现的次数为："+item.getValue());
            k++;
            if(k>=n){
                break;
            }
        }
    }
}
